package noodlesscm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;



public class IGui {
JButton IGuibutton1,IGuibutton2,IGuibutton3,IGuibutton4,IGuibutton5;
JFrame frame;
JPanel panel1,panel2;
JLabel label1;
JTextArea ta=new JTextArea(15,40);
JScrollPane scrollPane = new JScrollPane(ta);
AddInventory ai=new AddInventory();
AddProduct ap=new AddProduct();
ProductInventory pi=new ProductInventory();
ProductSelected ps1=new ProductSelected();

//Variables for file Reading Operation
FileReader frs;
BufferedReader br;

	
		
//Variables for inventory
        String id[]=new String[20];
        String name[]=new String[20],s,s1;
        String cline[]=new String[10];
        String qua[]=new String[20];
        String price[]=new String[20];
        String description[]=new String[20];


Display d=new Display();

	public IGui()
	{
		frame=new JFrame("SUPPLY CHAIN MANAGEMENT - Noodle House");
		frame.setLayout(new BorderLayout());

		label1=new JLabel("Welcome to Noodle House :");
                label1.setFont(new Font("calibri", Font.BOLD, 29));
                frame.add(label1,BorderLayout.NORTH);

		IGuibutton1=new JButton("View Menu");
		IGuibutton2=new JButton("Inventory Report");
		IGuibutton3=new JButton("Available Inventory Items");
		IGuibutton4=new JButton("Add Menu");
		IGuibutton5=new JButton("Add Inventory Item");
                
                IGuibutton1.addActionListener(new viewProd());
                IGuibutton2.addActionListener(new invreport());
                IGuibutton3.addActionListener(new currInv());
                IGuibutton4.addActionListener(new addProd());
                IGuibutton5.addActionListener(new addInv());
                

                frame.add(scrollPane,BorderLayout.CENTER);

         
		
                panel1=new JPanel(new GridLayout(2, 2));
		panel1.add(IGuibutton1);
		panel1.add(IGuibutton2);
		panel1.add(IGuibutton3);
		panel1.add(IGuibutton4);
		panel1.add(IGuibutton5);
                frame.add(panel1,BorderLayout.SOUTH);

                ta.setText("Console Window");
                ta.setBorder(BorderFactory.createLineBorder(Color.black, 1));

                frame.setVisible(true);
		frame.setSize(600,400);
		frame.setLocation(150,150);
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

       

        class viewProd implements ActionListener {

        public void actionPerformed(ActionEvent event)
        {
          
             viewProducts1();
             ta.append("Opening the menu of Noodle King \n");
        }
        }
        class invreport implements ActionListener {

        public void actionPerformed(ActionEvent event)
        {
           // pi.displayInventory();
            ps1.gettingProduct();
        }
        }
        class addInv implements ActionListener {

        public void actionPerformed(ActionEvent event)
        {
           ai.addInventory();
        }
        }
        class currInv implements ActionListener {

        public void actionPerformed(ActionEvent event)
        {
           currentInventory1();
           ta.append("Items in Inventory(Kitchen & store room) \n");
        }


        }
        class addProd implements ActionListener {

        public void actionPerformed(ActionEvent event)
        {
            ap.addProduct();
        }
        }

        /**
         * The function that is used for viewing the available menu  or products
         * in the kitchen
         * It contains the Name,ID,quantity,description and Inventories needed for
         * the particular product
         * @param args
         */
        public void viewProducts1()
        {
                                int id1[]= new int[20],i=0,lines=0;
                                try
                                {
                                    frs=new FileReader("GUIProduct.txt");
                                    br=new BufferedReader(frs);
                                    while((s1=br.readLine()) != null)
                                        {
                                            lines=lines+1;
                                        }
                                }
                                catch(Exception ex)
                                {
                                    System.out.println("Error in creating objects - Product");
                                }

                                ArrayList<Product> p=new ArrayList<Product>();
                                for(int j=0;j<lines;j++)
                                {
                                p.add(new Product());
                                }
                               
                                try
                                {
                                    frs=new FileReader("GUIProduct.txt");
                                    br=new BufferedReader(frs);

                                    while((s=br.readLine()) != null)
                                    {
                                        cline=s.split(",");
                                        p.get(i).setName(cline[0]);
                                        p.get(i).setProductid(Integer.parseInt(cline[1]));
                                        p.get(i).setQuantity(Integer.parseInt(cline[2]));
                                        p.get(i).setDescription(cline[3]);
                                        p.get(i).setInvenlist(cline[4]);
                                        i++;

                                    }
                                    Object[] col = new Object[]{"Name","ProductID","Quantity","Description","InventoryList"};
                                    Object[][] data = {};
                                    DefaultTableModel model = new DefaultTableModel(data,col);
                                    JTable table = new JTable(model);
                                    JScrollPane pane = new JScrollPane(table);


                                    for(int n=0;n<i;n++)
                                        {
                                            //System.out.println("Name :"+p.get(n).getName()+" ID "+p.get(n).getProductid()+" Quality "+p.get(n).getQuantity()+" Description "+p.get(n).getDescription()+" Inventory list "+p.get(n).getInvenlist());
                                            Object[] row1=new Object[]{p.get(n).getName(),p.get(n).getProductid(),p.get(n).getQuantity(),p.get(n).getDescription(),p.get(n).getInvenlist()};
                                            model.addRow(row1);
                                            
                                        }
                                    ta.append("Viewing Menu \n");
                                    ta.getCursor();
                                    JFrame jfp=new JFrame("Menu List");
                                    jfp.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                                    jfp.setBackground(Color.white);
                                    jfp.setSize(600,300);
                                    jfp.setVisible(true);
                                    jfp.setLocation(200,200);
                                    jfp.add(pane);

                                }

		catch(Exception ex)
		{
			System.out.println("Error in view product");

		}
        }

        /**
         * The function that is used for viewing the Inventory items 
         * the store room and Kitchen
         * It contains the Name,ID,quantity,and price of the item
         * @param args
         */
        public void currentInventory1()
        {
            {
                                int id1[]= new int[20],i=0,lines=0;

                                try
                                {
                                    frs=new FileReader("GUIItem.txt");
                                    br=new BufferedReader(frs);
                                    while((s1=br.readLine()) != null)
                                        {
                                            lines=lines+1;
                                        }
                                }
                                catch(Exception ex)
                                {
                                    System.out.println("Error in creating objects - Inventory");
                                }

                                ArrayList<Inventory> q=new ArrayList<Inventory>();

                                for(int j=0;j<lines;j++)
                                {
                                q.add(new Inventory());
                                }
                                

                                try
                                {
                                    frs=new FileReader("GUIItem.txt");

                                    br=new BufferedReader(frs);

                                    while((s1=br.readLine()) != null)
                                    {
                                        cline=s1.split(",");
                                        q.get(i).setName(cline[0]);
                                        q.get(i).setInventoryid(Integer.parseInt(cline[1]));
                                        q.get(i).setQuantity(Integer.parseInt(cline[2]));
                                        q.get(i).setPrice(Integer.parseInt(cline[3]));
                                        i++;
                                        
                                    }
                                    Object[] col = new Object[]{"Name","InventoryID","Quantity","Price"};
                                    Object[][] data = {};
                                    DefaultTableModel model = new DefaultTableModel(data,col);
                                    JTable table = new JTable(model);
                                    JScrollPane pane = new JScrollPane(table);



                                     for(int n=0;n<i;n++)
                                      {
                                        //System.out.println("Name :"+q.get(n).getName()+" ID "+q.get(n).getInventoryid()+" Quality "+q.get(n).getQuantity()+" Price "+q.get(n).getPrice());
                                        Object[] row1=new Object[]{q.get(n).getName(),q.get(n).getInventoryid(),q.get(n).getQuantity(),q.get(n).getPrice()};
                                        model.addRow(row1);
                                        
                                     }
                                    JFrame jfr=new JFrame("Items in Inventory(Kitchen & store room) ");
                                    jfr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                                    jfr.setBackground(Color.white);
                                    jfr.setSize(600,300);
                                    jfr.setVisible(true);
                                    jfr.setLocation(200,200);
                                    jfr.add(pane);

		}
		catch(Exception ex)
		{
			System.out.println("Error in inventory");

		}
        }}
        public void currentInventory()
	{
	   try {

                XMLDecoder istream = new XMLDecoder(new BufferedInputStream(new FileInputStream("inventory.xml")));
                InventoryList obj =(InventoryList) istream.readObject();
                istream.close();

		Inventory p=new Inventory();

		Object[] col = new Object[]{"Name","InventoryId","Quantity","Price"};
		Object[][] data = {};
		DefaultTableModel model = new DefaultTableModel(data,col);
		JTable table = new JTable(model);
		for(int i=0;i<obj.inventories.size();i++)
                    {
                        p=(Inventory) obj.inventories.get(i);
                        Object[] row1=new Object[]{p.getName(),p.getInventoryid(),p.getQuantity(),p.getPrice()};
                        model.addRow(row1);
                    }
		JScrollPane pane = new JScrollPane( table );
		JFrame jfr=new JFrame("Available Inventory List");
		jfr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		jfr.setBackground(Color.white);
		jfr.setSize(600,300);
        	jfr.setVisible(true);
		jfr.setLocation(200,200);
		jfr.add(pane);

		}

                catch(Exception ex)
                {
                    System.out.println("In current error");
         	}
	}
        public void viewProducts()
	{

         try {

                XMLDecoder istream = new XMLDecoder(new BufferedInputStream(new FileInputStream("products.xml")));
                ProductList obj =(ProductList) istream.readObject();
                istream.close();

                Product p=new Product();

                Object[] col = new Object[]{"Name","ProductId","Quantity","Description","Inventory List"};
                Object[][] data = {};
                DefaultTableModel model = new DefaultTableModel(data,col);
                JTable table = new JTable(model);
                    for(int i=0;i<obj.products.size();i++)
                        {
                            p=(Product) obj.products.get(i);
                            Object[] row1=new Object[]{p.getName(),p.getProductid(),p.getQuantity(),p.getDescription(),p.getInvenlist()};
                            model.addRow(row1);
                        }

                JScrollPane pane = new JScrollPane( table );
                JFrame jfr=new JFrame("Available Products List");
                jfr.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                jfr.setBackground(Color.white);
                jfr.setSize(600,300);
                jfr.setVisible(true);
                jfr.setLocation(200,200);
                jfr.add(pane);

            }

            catch(Exception ex)
                {
                    System.out.println("null error");
                }
	}

        /**
         * The main function to call the Graphical user interface
         * This main function calls the Gui
         * @param args
         */
	public static void main(String args[])
	{
	IGui i=new IGui();
	}
}
